﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using pHMb.Spectrogram;

namespace Lung_Sounds_V2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        RealTimeAudioProcesser _aProcessor;

        private SpectrogramPane spectrogramPane1;
        public MainWindow()
        {
            InitializeComponent();

            spectrogramPane1 = new SpectrogramPane();
            spectrogramPane1.Dock = System.Windows.Forms.DockStyle.Left;
            spectrogramPane1.AxesColor = System.Drawing.Color.WhiteSmoke;
            spectrogramPane1.BackColor = System.Drawing.Color.DimGray;
            spectrogramPane1.Divisor = 130;
            spectrogramPane1.isGreyScale = false;
            spectrogramPane1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 0);
            spectrogramPane1.Width = 500;

            spectPanel.Controls.Add(spectrogramPane1);
            spectPanel.Scroll += spectPanel_Scroll;
            spectPanel.AutoScroll = true;
        }

        void spectPanel_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
        {
            spectrogramPane1.ScrollVPWidth = spectPanel.Width;
            spectrogramPane1.ScrollHorizOffset = - spectPanel.DisplayRectangle.X;
            spectrogramPane1.UpdateOverlay();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _aProcessor = new RealTimeAudioProcesser(this, spectrogramPane1);
            _aProcessor.StartLive();
        }

        private void checkBoxFileInput_Unchecked(object sender, RoutedEventArgs e)
        {
            _aProcessor.StartLive();
        }

        private void checkBoxFileInput_Checked(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.InitialDirectory = @"C:\Users\mrmt32\Dropbox\MPHYS Project\Breath Sounds\wav files\";
            openDialog.Filter = "Wave Files (*.WAV)|*.WAV";

            if (openDialog.ShowDialog() == true)
            {
                if (!_aProcessor.LoadWave(openDialog.FileName, 4096))
                {
                    checkBoxFileInput.IsChecked = false;
                }
                else
                {
                    txtFileInput.Text = openDialog.FileName;
                }
            }
            else
            {
                checkBoxFileInput.IsChecked = false;
            }
        }

        private void buttonSaveSpect_Click(object sender, RoutedEventArgs e)
        {
            _aProcessor.SaveSpectrogram();
        }

        private void buttonRefreshSpect_Click(object sender, RoutedEventArgs e)
        {

            _aProcessor.Reset();
            if ((bool)checkBoxFileInput.IsChecked)
            {
                _aProcessor.LoadWave(txtFileInput.Text, 4096);
            }
        }

        private void checkBoxFiltering_Click(object sender, RoutedEventArgs e)
        {
            _aProcessor.EnableFiltering = (bool)checkBoxFiltering.IsChecked;
        }

        private void txtDevisor_TextChanged(object sender, TextChangedEventArgs e)
        {
            int divisor;

            if (int.TryParse(txtDevisor.Text, out divisor) && _aProcessor != null)
            {
                _aProcessor.Divisor = divisor;
            }
        }

        private void txtLimiter_TextChanged(object sender, TextChangedEventArgs e)
        {
            int limit;

            if (int.TryParse(txtLimiter.Text, out limit) && _aProcessor != null)
            {
                _aProcessor.LimitPercent = limit;
            }
        }

        private void buttonRecord_Click(object sender, RoutedEventArgs e)
        {
            if (buttonRecord.IsChecked == true)
            {
                if (!_aProcessor.StartRecord())
                {
                    buttonRecord.IsChecked = false;
                }
            }
            else
            {
                _aProcessor.StopRecord();
            }
        }

        private void buttonLog_Checked(object sender, RoutedEventArgs e)
        {
            if (_aProcessor != null)
            {
                _aProcessor.isSpectrogramLog = true;
            }
        }

        private void buttonLog_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_aProcessor != null)
            {
                _aProcessor.isSpectrogramLog = false;
            }
        }

        private void comboWindowSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_aProcessor != null)
            {
                if ((bool)checkBoxFileInput.IsChecked)
                {
                    _aProcessor.Reset();
                    _aProcessor.LoadWave(txtFileInput.Text, (int)Math.Pow(2, int.Parse((string)((ComboBoxItem)comboWindowSize.SelectedItem).Content)));
                }
                else
                {
                    _aProcessor.Stop();
                    _aProcessor.Start((int)Math.Pow(2, int.Parse((string)((ComboBoxItem)comboWindowSize.SelectedItem).Content)));
                    _aProcessor.Reset();
                }
            }
        }

        private void buttonPause_Click(object sender, RoutedEventArgs e)
        {
            _aProcessor.isPaused = (bool)buttonPause.IsChecked;
        }

        private void buttonGrey_Click(object sender, RoutedEventArgs e)
        {
            _aProcessor.isGreyscale = (bool)buttonGrey.IsChecked;
        }

        private void comboWindowType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_aProcessor != null)
            {
                switch ((string)((ComboBoxItem)comboWindowType.SelectedItem).Content)
                {
                    case "Hann":
                        _aProcessor.WindowType = RealTimeAudioProcesser.WindowTypes.Hann;
                        break;

                    case "Hamming":
                        _aProcessor.WindowType = RealTimeAudioProcesser.WindowTypes.Hamming;
                        break;

                    case "Rectangle":
                        _aProcessor.WindowType = RealTimeAudioProcesser.WindowTypes.Rectangle;
                        break;

                    case "Gaussian":
                        _aProcessor.WindowType = RealTimeAudioProcesser.WindowTypes.Gaussian;
                        break;
                }
            }
        }

        private void buttonOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Multiselect = true;
            openDialog.Filter = "Comma delimited file (*.CSV)|*.CSV|All Files (*.*)|*.*";

            if (openDialog.ShowDialog() == true)
            {
                new GraphWindow(openDialog.FileNames).Show();
            }

        }

        private void Window_Closed_1(object sender, EventArgs e)
        {
            _aProcessor.Close();
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _aProcessor.Stop();
        }

        private void buttonCombine_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openWavDialog = new OpenFileDialog();

            openWavDialog.InitialDirectory = @"C:\Users\mrmt32\Dropbox\MPHYS Project\Breath Sounds\wav files\";
            openWavDialog.Filter = "Wave Files (*.wav)|*.wav";
            openWavDialog.Multiselect = true;

            if (openWavDialog.ShowDialog() == true)
            {
                SaveFileDialog saveWavDialog = new SaveFileDialog();

                saveWavDialog.Filter = "Wave Files (*.wav)|*.wav";

                if (saveWavDialog.ShowDialog() == true)
                {
                    _aProcessor.CombineWaves(openWavDialog.FileNames, saveWavDialog.FileName);
                    _aProcessor.LoadWave(saveWavDialog.FileName);
                }
            }
        }

        private void ScrollViewer_ScrollChanged_1(object sender, ScrollChangedEventArgs e)
        {
        }
  
    }
}
